﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using condortravel.operaciones.core.DataModel;
using System.Data.Linq;

namespace condortravel.operaciones.persistence.operacionesImpl
{
    public class EventoDAOImpl: condortravel.operaciones.persistence.Impl.GenericDAO<Tb_Evento,long>
    {
        public override Tb_Evento update(Tb_Evento entity)
        {
            Tb_Evento evento;
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }

                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    evento = dataContext.GetTable<Tb_Evento>().Where(p => p.evento_ID == entity.evento_ID).Single();
                    evento.observacion = entity.observacion;
                    evento.descripcion = entity.descripcion;
                    evento.estadoEvento_ID = entity.estadoEvento_ID;
                    evento.fechaEvento = entity.fechaEvento;
                    evento.hora = entity.hora;
                    evento.programacion_ID = entity.programacion_ID;
                    evento.agencia_ID = entity.agencia_ID;
                    dataContext.SubmitChanges();
                }
            }
            return evento;
            

        }

        public override Tb_Evento getByID(long id)
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    DataLoadOptions options = new DataLoadOptions();
                    options.LoadWith<Tb_Evento>(s => s.Tb_ServiciosProgramado);
                    options.LoadWith<Tb_ServiciosProgramado>(s => s.Tb_ItinerarioViajero);
                    options.LoadWith<Tb_ItinerarioViajero>(s => s.Tb_Cliente);
                    dataContext.LoadOptions = options;
                    return (from p in dataContext.GetTable<Tb_Evento>()
                            where p.evento_ID == id
                            select p).SingleOrDefault<Tb_Evento>();

                }
            }
        }

        public override void delete(long pk)
        {
            string deleteDetails = string.Format("delete  from Tb_Evento where evento_id='{0}'", pk);
            this.executeSQL(deleteDetails);
        }

        public List<Tb_Evento> getListEventos(long codigoProgramacion)
        {
            using (ImpersonationHelper imp = new ImpersonationHelper(this.Impersonation.User, this.Impersonation.Domain, this.Impersonation.Password))
            {
                if (IsImpersonate)
                {
                    if (!imp.ImpersonateUser())
                    {
                        throw new Exception("The Impersonate has failed");
                    }

                }
                using (DataContext dataContext = this.getInstanceDataContextFactory())
                {
                    DataLoadOptions options = new DataLoadOptions();
                    options.LoadWith<Tb_Evento>(s => s.Tb_ServiciosProgramado);
                    options.LoadWith<Tb_Evento>(s => s.Tb_Estado_Evento);
                    options.LoadWith<Tb_ServiciosProgramado>(s => s.Tb_ItinerarioViajero);
                    options.LoadWith<Tb_ItinerarioViajero>(s => s.Tb_Cliente);
                    dataContext.LoadOptions = options;
                    return (from p in dataContext.GetTable<Tb_Evento>()
                            where p.programacion_ID == codigoProgramacion
                            select p).ToList<Tb_Evento>();
                }
            }
        }
    }
}
